(function() {
	var oldDIVWidth;
	var userRight;
    $("body").on("click", ".menu-scd-click", function (){
        var id=$(this).attr('id');
        var s=$(this).attr("href");
        $("#menu-scd").popup("close");
        if(id=="goTop"){
        	$('html,body').animate({scrollTop: '0px'}, 1000);return false;
        }else if(id=="goBottom"){
        	$('html,body').animate({scrollTop:$(document).height()+'px'},1000);return false;
        }/*else if(id=="goaAnds"){
        	$('html,body').animate({scrollTop: '0px'}, 1000);
        }*/
	});
	$("body").on("click", ".menuClick", function (){
		$("#menu-scd-list").empty();
		/*alert($(window).height()); //浏览器当前窗口可视区域高度 
		alert($(document).height()); //浏览器当前窗口文档的高度 
		alert($(document.body).height());//浏览器当前窗口文档body的高度 
		alert($(document.body).outerHeight(true));*/
		var id=$(this).attr("id");
		if(id=="menu-login"){
			$("#popupLoginSelect").popup("open");
		}else{
			if(id=="menu-home"){
				window.location.href = window.rootPath + "/ui/mobileMain?device=mobile&identify=";
			}else{
				var contenthtml="";
				
				for(var i=0;i<userRight.length;i++){
					if(userRight[i].mobile!=undefined&&userRight[i].mobile=="true"&&userRight[i].target!="#"&&userRight[i].parentId==id){
						var url='javascript: ;';
						var isGo=false;
						var menuType="menuClick";
						var icon="home";
						var dataajax ='';
						if(userRight[i].icon!=undefined&&userRight[i].icon!=""){
							icon=userRight[i].icon;
						}
						if(userRight[i].target.indexOf("?") != -1){
							url=window.rootPath+userRight[i].target+"&device=mobile&identify="+$("#identify").val();
						}else{
							url=window.rootPath+userRight[i].target+"?device=mobile&identify="+$("#identify").val();
						}
					    isGo=true;
					    menuType="menu-scd-click-bak";
					    dataajax ='data-ajax="false"';
					    contenthtml +='<li class="ui-li-icon ui-corner-none" data-icon="false"><a href="'+url+'" data-icon="'+icon+'" class="'+menuType+'" id="'+userRight[i].id+'" '+dataajax+'>'+userRight[i].moduleName+'</a></li>';
					}
				}
				if(contenthtml!=""){
					$("#menu-scd-list").append(contenthtml);
					$("#menu-scd-list").listview('refresh');
				}
				openGroupMenu(id,"menu-scd","menu-scd-arrow");
			}
			
		}
	});
	
	/*计算菜单位置*/
	function openGroupMenu(controller,controllerscd,controllerscdarrow){
	    var left, top;
	    var rect = document.getElementById(controller).getBoundingClientRect();//controller.getBoundingClientRect();
	    var winrect =document.getElementById(controllerscd).getBoundingClientRect();//controllerscd.getBoundingClientRect();
	    left = rect.left+ winrect.width/2;
	    top = rect.top-winrect.height/2+$(document).scrollTop();
	    $("#"+controllerscd).popup("open", {x:left, y:top});
	    /*计算三角型的位置*/
	    winrect =document.getElementById(controllerscd).getBoundingClientRect();
	    document.getElementById(controllerscdarrow).style.left = (rect.left + rect.width/2 - winrect.left) + "px";
	};
	
	var defaultUserPic = "../img/defaultUserPic.png";
	$("#userPic").error(function() {
		$(this).attr("src", defaultUserPic);
		$(this).error = null;
	});
	var personalType;
	$(document).ready(function() {
		personalType = jQuery('#personalType').val();
		if(personalType == ''){//默认游客界面
			personalType = "1";
		}
		getData();
		var userPic = $("#userPicUrl").val();
		
		if(userPic==undefined||userPic==""){
			userPic=defaultUserPic;
		}else{
			userPic=window.rootPath+'/'+userPic;
		}
		$("#userPic").attr("src",userPic);
		
		getUserContent();
	});
	/**
	 * 初始化数据-页面菜单
	 */
	function getData() {
		var url = window.rootPath + '/ui/mobileMain/getData';
		jQuery.ajax({
			url : encodeURI(url),
			cache : false,
			type : "get",
			data : "",
			dataType : "json",
			contentType : 'application/json;charset=utf-8',// 解决返回数据中的中文乱码问题
			success : function(data) {
				//#动态添加菜单
				var contenthtml='<ul id="menu-list">'
				    +'<li><a href="javascript: ;" data-icon="home" class="menuClick"'
					+'id="menu-home">首页</a></li>'
					+'<li><a href="javascript: ;" data-icon="user" class="menuClick"'
					+'id="menu-login">登录</a></li>';
				userRight =data.modules;
				for(var i=0;i<data.modules.length;i++){
					if(data.modules[i].mobile!=undefined&&data.modules[i].mobile=="true"&&data.modules[i].menuLevel==1){//&&data.modules[i].menuLevel==1
						var url='javascript: ;';
						var isGo=false;
						var menuType="menuClick";
						var icon="home";
						var dataajax ='';
						if(data.modules[i].icon!=undefined&&data.modules[i].icon!=""){
							icon=data.modules[i].icon;
						}
						
						if(data.modules[i].target!="#"){
							if(data.modules[i].target.indexOf("?") != -1){
								url=window.rootPath+data.modules[i].target+"&device=mobile&identify="+$("#identify").val();
							}else{
								url=window.rootPath+data.modules[i].target+"?device=mobile&identify="+$("#identify").val();
							}
						    isGo=true;
						    menuType="menu-scd-click-bak";
						    dataajax ='data-ajax="false"';
						}
						 contenthtml +='<li><a href="'+url+'" data-icon="'+icon+'" class="'+menuType+'" id="'+data.modules[i].id+'" '+dataajax+'>'+data.modules[i].moduleName+'</a></li>';
					}
				}
				 contenthtml+='</ul>';
				 $('#menu-bar').empty();
				 $('#menu-bar').append(contenthtml);
				 $('#menu-bar').navbar('refresh');
			}
		});
	}
	
	/**
	 * 初始化数据-页面菜单
	 */
	function getNewData() {
		//#动态添加菜单
		var contenthtml='<ul id="menu-list">'
		    +'<li><a href="javascript: ;" data-icon="home" class="menuClick"'
			+'id="menu-home">首页</a></li>'
			+'<li><a href="javascript: ;" data-icon="user" class="menuClick"'
			+'id="menu-login">登录</a></li>';
		 contenthtml+='</ul>';
		 $('#menu-bar').empty();
		 $('#menu-bar').append(contenthtml);
		 $('#menu-bar').navbar('refresh');
	}
	
	/**
	 * 初始化用户主体信息
	 */
	function getUserContent(){
		var url = window.rootPath + '/ui/Personal/getUser/'+personalType;
		jQuery.ajax({
			url : encodeURI(url),
			cache : false,
			type : "get",
			data : "",
			dataType : "json",
			contentType : 'application/json;charset=utf-8',// 解决返回数据中的中文乱码问题
			success : function(data) {
				jQuery('#oneNum').text(data.oneNum);
				jQuery('#oneTitle').text(data.oneTitle);
				jQuery('#twoNum').text(data.twoNum);
				jQuery('#twoTitle').text(data.twoTitle);
				jQuery('#threeNum').text(data.threeNum);
				jQuery('#threeTitle').text(data.threeTitle);
				
				var content = data.content;
			
				if(personalType == 1){	//游客 content
					setVisitorContent(content);
				}else if(personalType == 2){	 //城管 content
					setManageContent(content);
				}else if(personalType == 3){  //领导 content
					setLeaderContent(content);
				}
			}
		})
	}
	/**
	 * 设置主体信息显示
	 */
	function setContentShow(visitor,manage,leader){
		if(visitor){
			jQuery('#visitorTabs').show();
		}else{
			jQuery('#visitorTabs').hide();
		}
		if(manage){
			jQuery('#manageTabs').show();
		}else{
			jQuery('#manageTabs').hide();
		}
		if(leader){
			jQuery('#leaderTabs').show();
		}else{
			jQuery('#leaderTabs').hide();
		}
		
	};
	
	/**
	 * 游客信息设置
	 */
	function setVisitorContent(data){
		var reports = data.reports;
		buildVisitorListView(reports,"#visitorOneListView");
		var deals = data.deals;
		buildVisitorListView(deals,"#visitorTwoListView");
		var finisheds = data.finisheds;
		buildVisitorListView(finisheds,"#visitorThreeListView");
		setContentShow(true,false,false);
	}
	
	/**
	 * 城管信息设置
	 */
	function setManageContent(data){
		var receives = data.receives;
		buildManageListView(receives,"#receiveListView");
		setContentShow(false,true,false);
	}
	
	/**
	 * 领导信息设置
	 */
	function setLeaderContent(data){
		var reports = data.reports;
		buildLeaderListView(reports,"#leaderOneListView");
		var deals = data.deals;
		buildLeaderListView(deals,"#leaderTwoListView");
		var finisheds = data.finisheds;
		buildLeaderListView(finisheds,"#leaderThreeListView");
		setContentShow(false,false,true);
	}
	
	function buildVisitorListView(pageInfo, str) {
		$(str).empty();
		if (pageInfo.length == 0) {
			var contenthtml = '<li><h2 style="color:blue">暂无信息</h2></li>';
			$(str).append(contenthtml);
			$(str).listview('refresh');
		}
		for (var i = 0; i < pageInfo.length; i++) {
			var purl = pageInfo[i].reportImgAdress;

			if (purl == undefined || purl == "") {
				purl = "img/nopicture.png";
			}
			purl = window.rootPath + "/" + purl;
			var contenthtml='<li><a style="background-color:#FFFFFF;" href="#" data-transition="slide" class="visitorClick" defineId="'+pageInfo[i].id+'" id="'+pageInfo[i].id+'">'
			+ '<img src="'+purl+'" style="margin-top:1em;margin-left:1em;width:70px;height:70px"/>'
			+ '<h2 style="color: #F44C31;">'+moment(pageInfo[i].reportAccTime).format('YYYY-MM-DD HH:mm')+'&nbsp;&nbsp;&nbsp;&nbsp;'+ pageInfo[i].reportEventType.eventName+'</h2>'
			+ '<p style="color:#999999;">&nbsp;'+pageInfo[i].reportContent+'</p>'
			+ '<p style="color:#999999;">&nbsp;'+pageInfo[i].reportAdress+'</p>';
			contenthtml+='</a></li>';
			$(str).append(contenthtml);
		}
		$(str).listview('refresh');
		for(var i=0;i<pageInfo.length;i++){
			$("#"+pageInfo[i].id).removeClass("ui-btn-icon-right");
		}
	}

	function buildManageListView(pageInfo, str) {
		$(str).empty();
		if (pageInfo.length == 0) {
			var contenthtml = '<li><h2 style="color:blue">暂无信息</h2></li>';
			$(str).append(contenthtml);
			$(str).listview('refresh');
		}
		for (var i = 0; i < pageInfo.length; i++) {
			var purl = pageInfo[i].registerEventImgAdress;
			
			if (purl == undefined || purl == "") {
				purl = "img/nopicture.png";
			}
			purl = window.rootPath + "/" + purl;
			var contenthtml='<li><a style="background-color:#FFFFFF;" href="#" data-transition="slide" class="manageClick" defineId="'+pageInfo[i].id+'" id="'+pageInfo[i].id+'">'
			+ '<img src="'+purl+'" style="margin-top:1em;margin-left:1em;width:70px;height:70px"/>'
			+ '<h2 style="color: #F44C31;">'+moment(pageInfo[i].registerEventAccTime).format('YYYY-MM-DD HH:mm')+'&nbsp;&nbsp;&nbsp;&nbsp;'+ pageInfo[i].registerEventType.eventName+'</h2>'
			+ '<p style="color:#999999;">&nbsp;'+pageInfo[i].registerEventContent+'</p>'
			+ '<p style="color:#999999;">&nbsp;'+pageInfo[i].registerEventAdress+'</p>';
			contenthtml+='</a></li>';
			$(str).append(contenthtml);
		}
		$(str).listview('refresh');
		for(var i=0;i<pageInfo.length;i++){
			$("#"+pageInfo[i].id).removeClass("ui-btn-icon-right");
		}
	}
	
	function buildLeaderListView(pageInfo, str) {
		$(str).empty();
		if (pageInfo.length == 0) {
			var contenthtml = '<li><h2 style="color:blue">暂无信息</h2></li>';
			$(str).append(contenthtml);
			$(str).listview('refresh');
		}
		for (var i = 0; i < pageInfo.length; i++) {
			var purl = pageInfo[i].reportImgAdress;

			if (purl == undefined || purl == "") {
				purl = "img/nopicture.png";
			}
			purl = window.rootPath + "/" + purl;
			var contenthtml='<li><a style="background-color:#FFFFFF;" href="#" data-transition="slide" class="leaderClick" defineId="'+pageInfo[i].id+'" id="'+pageInfo[i].id+'">'
			+ '<img src="'+purl+'" style="margin-top:1em;margin-left:1em;width:70px;height:70px"/>'
			+ '<h2 style="color: #F44C31;">'+moment(pageInfo[i].reportAccTime).format('YYYY-MM-DD HH:mm')+'&nbsp;&nbsp;&nbsp;&nbsp;'+ pageInfo[i].reportEventType.eventName+'</h2>'
			+ '<p style="color:#999999;">&nbsp;'+pageInfo[i].reportContent+'</p>'
			+ '<p style="color:#999999;">&nbsp;'+pageInfo[i].reportAdress+'</p>';
			contenthtml+='</a></li>';
			$(str).append(contenthtml);
		}
		$(str).listview('refresh');
		for(var i=0;i<pageInfo.length;i++){
			$("#"+pageInfo[i].id).removeClass("ui-btn-icon-right");
		}
	}
	
	function cleanReportDetail(){
		jQuery('#reportEventVariable').text('');
		jQuery('#reportEventType').text('');
		jQuery('#reportContent').text('');
		jQuery('#reportMemo').text('');
		var pic = rootPath + "/img/nopicture.png";
		jQuery('#avatart').attr("src",pic);
		jQuery('#locationDes').text('');
	}
	
	$("body").on("click", ".visitorClick", function (){
		var id = $(this).attr("defineId");
		cleanReportDetail();
		$.ajax({
			url : rootPath + "/ui/Personal/findEventReport/"+id,
			dataType : 'json',
			type : 'get',
			success : function(result) {
				var data = result.eventReport;
				jQuery('#reportEventVariable').text(data.reportEventVariable.title);
				jQuery('#reportEventType').text(data.reportEventType.eventName);
				jQuery('#reportContent').text(data.reportContent);
				jQuery('#reportMemo').text(data.reportMemo);
				var pic = data.reportImgAdress;
				if(pic == ""){
					pic =  rootPath + "/img/nopicture.png";
				}else{
					pic = rootPath +'/' + pic;
				}
				jQuery('#avatart').attr("src",pic);
				jQuery('#locationDes').text(data.reportAdress);
				
			}
		});
		$(this).prop("href", "#reportDetail");
	});

	function cleanRegisterDetail(){
		jQuery('#reportEventVariable').text('');
		jQuery('#reportEventType').text('');
		jQuery('#reportContent').text('');
		jQuery('#reportMemo').text('');
		var pic = rootPath + "/img/nopicture.png";
		jQuery('#avatart').attr("src",pic);
		jQuery('#locationDes').text('');
	}
	
	$("body").on("click", ".manageClick", function (){
		var id = $(this).attr("defineId");
		cleanRegisterDetail();
		$.ajax({
			url : rootPath + "/ui/Personal/findEventRegister/"+id,
			dataType : 'json',
			type : 'get',
			success : function(result) {
				var data = result.eventRegister;
				jQuery('#registerEventType').text(data.registerEventType.title);
				jQuery('#registerEventAccTime').text(moment(data.registerEventAccTime).format('YYYY-MM-DD HH:mm'));
				jQuery('#registerEventContent').text(data.registerEventContent);
				jQuery('#registerEventMemo').text(data.registerEventMemo);
				var pic = data.registerEventImgAdress;
				if(pic == ""){
					pic =  rootPath + "/img/nopicture.png";
				}else{
					pic = rootPath +'/' + pic;
				}
				jQuery('#registerAvatart').attr("src",pic);
				jQuery('#locationDesRegister').text(data.registerEventAdress);
				
			}
		});
		$(this).prop("href", "#registerDetail");
	});

	
	$("body").on("click", ".leaderClick", function (){
		var id = $(this).attr("defineId");
		cleanReportDetail();
		$.ajax({
			url : rootPath + "/ui/Personal/findEventReport/"+id,
			dataType : 'json',
			type : 'get',
			success : function(result) {
				var data = result.eventReport;
				jQuery('#reportEventVariable').text(data.reportEventVariable.title);
				jQuery('#reportEventType').text(data.reportEventType.eventName);
				jQuery('#reportContent').text(data.reportContent);
				jQuery('#reportMemo').text(data.reportMemo);
				var pic = data.reportImgAdress;
				if(pic == ""){
					pic =  rootPath + "/img/nopicture.png";
				}else{
					pic = rootPath +'/' + pic;
				}
				jQuery('#avatart').attr("src",pic);
				jQuery('#locationDes').text(data.reportAdress);
				
			}
		});
		$(this).prop("href", "#reportDetail");
	});
})();